// pages/admin/adminscan.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    olist: ['加', '减'],
    operate: true,
    Scaned: '',
    score: 0,
    showTopTips: false,
    errmsg: 'errmsg'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  showTopTips: function(str) {
    var that = this;
    this.setData({
      showTopTips: true,
      errmsg: str
    });
    setTimeout(function() {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  anaCode: function(str) {
    res = JSON.parse(str)
    // objectId:该二维码在服务端的id
    // operation:进行的操作 add subtract clear show
    // score:操作的分数

  },
  Scan: function() {
    let that = this

    if (that.data.score <= 0) {
      that.showTopTips('请输入正确的分数！')
      return
    }
    wx.scanCode({
      onlyFromCamera: true,
      scanType: 'qrCode',
      success: (res) => {
        // 成功扫码
        console.log('扫码结果：', res)
        that.setData({
          Scaned: res.result.split('?=')[1]
        })
        let cookie = wx.getStorageSync('cookieKey');
        let header = {};
        if (cookie) {
          header.Cookie = cookie;
        }
        var openid = that.data.Scaned;
        var score = that.data.score;
        if (!that.data.operate) score = -score;
        wx.request({
          url: app.globalData.url + '/wxapp/change_score',
          method: 'GET',
          header: header,
          data: {
            openid: that.data.Scaned,
            score: score,
          },
          success: (res) => {
            console.log('返回值：', res)
            wx.showToast({
              title: "成功 " + res.data + "！",
              duration: 4000
            })
          }
        })
      }
    })




  },
  scoreChange: function(e) {
    this.setData({
      score: parseInt(e.detail.value)
    })
    console.log(parseInt(e.detail.value))
  },
  switchChange: function(e) {
    this.setData({
      operate: e.detail.value
    })
    console.log(e.detail.value)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})